using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyHocPhi.Entity;
using QuanLyHocPhi.DAL;
using QuanLyHocPhi.Util;

namespace QuanLyHocPhi
{
    public partial class frmThuphi : DevComponents.DotNetBar.Metro.MetroForm
    {
        LOP_HOC_DAO lhDao = new LOP_HOC_DAO();
        SINH_VIEN_DAO svDao = new SINH_VIEN_DAO();
        IList<LOP_HOC> lhoc = null;
        IList<SINH_VIEN> svien = null;
        IList<HOA_DON> dshoadon = null;
        HOA_DON_DAO hdDao = new HOA_DON_DAO();
        HOA_DON hd = new HOA_DON();
        IList<MUC_PHI> dsmucphi = null;
        MUC_PHI_DAO mpDao = new MUC_PHI_DAO();
        MUC_PHI mp = new MUC_PHI();
        IList<HOA_DON_CT> dshdct = null;
        HOA_DON_CT hdct = new HOA_DON_CT();
        HOA_DON_CT_DAO hdctDao = new HOA_DON_CT_DAO();
        DOI_TUONG doiTuongduocchon = new DOI_TUONG();
        BIEN_LAI_DAO daoBienLai = new BIEN_LAI_DAO();
        float soTienYeuCau = 0;
        public frmThuphi()
        {
            InitializeComponent();
        }

       

        private void frmThuphi_Load(object sender, EventArgs e)
        {
            lhoc = lhDao.GetCurrentLOPHOC();
            foreach (LOP_HOC lh in lhoc) 
            {
                cbbLop.Items.Add(lh);
            }
            //Nap du lieu SINH_VIEN len datagirdview
            //svien = svDao.GetStudentsByNameOrCode("%");
           // dtgvThongtin.AutoGenerateColumns = false;
           // dtgvThongtin.DataSource = svien;
            int maLop = -1;
            if (cbbLop.SelectedItem != null)
            {
                maLop = ((LOP_HOC)cbbLop.SelectedItem).MA_LOP_HOC;
            }
           // dshoadon = hdDao.GetActivatedInvoicesHadClass(txtTensinhvien.Text,maLop);
            dshoadon = hdDao.GetActivatedInvoices("%", -1);
           
            dtgvThongtin.AutoGenerateColumns = false;
            objectBindingSource1.DataSource = dshoadon;
            dtgvThongtin.DataSource = objectBindingSource1;


            dtgvThongtin.AllowUserToAddRows = false;
            dtgvThongtin.AllowUserToDeleteRows = false;
        }

        private void dtgvThongtin_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dtgvThongtin_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = dtgvThongtin.CurrentRow.Index;
            if (row < 0 || row >= dtgvThongtin.RowCount) return;
            hd = dshoadon[row];
            if (dshoadon[row].SINH_VIEN.MA_SO_SV != null)
           {
                txtMasosv.Text = dshoadon[row].SINH_VIEN.MA_SO_SV;
            }

            txtTensv.Text = dshoadon[row].SINH_VIEN.TEN_SV;
            txtLop.Text = dshoadon[row].MA_LOAI_LOP.LOP;

            dshdct = hdctDao.GetSubInvoicesByInvoice(hd.MA_HOA_DON);
            
            float soTienDaNop = 0;
            
            DateTime ngayDauChuaThanhToan = DateTime.Now;
            foreach (HOA_DON_CT ct in dshdct)
            {
                if (ct.TRANG_THAI)
                {
                    soTienDaNop = soTienDaNop += ct.SO_TIEN;
                }
                else
                {
                    if (ngayDauChuaThanhToan == DateTime.Now)
                    {
                        ngayDauChuaThanhToan = ct.NGAY_THU_PHI;
                    }
                }
                if (ct.NGAY_THU_PHI < DateTime.Now && ct.TRANG_THAI == false)
                {
                    soTienYeuCau += ct.SO_TIEN;
                }
                
            }

          mp = mpDao.GetFeeLevelById(hd.MA_MUC_PHI.MA_MUC_PHI);
          txtTongtien.Text = mp.TONG_TIEN.ToString();
          txtSotientt.Text = soTienDaNop.ToString();
          txtSotienyc.Text = soTienYeuCau.ToString();
          txtQuahan.Text = DateTime.Now.Subtract(ngayDauChuaThanhToan).Days.ToString();
          txtSotiencl.Text = (mp.TONG_TIEN - soTienDaNop).ToString();
          txtSolantt.Text = mp.SO_LAN_THANH_TOAN.ToString();
          txtDangkynhom.Text = hd.DANG_KY_NHOM.ToString();
          //txtTongtien.Text = dshoadon[e.RowIndex].MA_MUC_PHI.TONG_TIEN.ToString();
          hd.MA_MUC_PHI = mp;
          foreach (LOP_HOC lop in lhoc)
          {
              if (lop.MA_LOP_HOC == hd.MA_LOP_HOC.MA_LOP_HOC)
              {
                  txtLop.Text = lop.TEN_LOP_HOC;
                  break;
              }
          }

            //nap bang
          dtgvHocphi.AutoGenerateColumns = false;
          dtgvHocphi.DataSource = dshdct;
          //nap bang Bien Lai
          IList<BIEN_LAI> dsBienLai = daoBienLai.GetReceiptsByInvoice(hd.MA_HOA_DON);
          dtgvBienlai.AutoGenerateColumns = false;
          dtgvBienlai.AllowUserToAddRows = false;
          dtgvBienlai.AllowUserToDeleteRows = false;
          dtgvBienlai.DataSource = dsBienLai;
        }

        private void btnHocphi_Click(object sender, EventArgs e)
        {                 
            frmThanhtoan frm = new frmThanhtoan(hd,soTienYeuCau);
            frm.Show();
           if(dtgvThongtin.CurrentRow.Index>=0) dtgvThongtin_CellClick(sender, null);
        }



        private void buttonX2_Click(object sender, EventArgs e)
        {
            frmThanhtoanphidt frm = new frmThanhtoanphidt(hd,0);
            frm.Show();
        }

        

       
    }
}